#!/usr/bin/env bash
# uppnighty2deb
# From a 'nighty build' source tree, batch builds theide and creates .deb package
#

######################################################################
#convert a path to absolute one
rel2abs()
{
    # make sure file is specified
    if [ -z "$1" ]
    then
       echo "$0 <path>"
       return 1
    fi

    # already absolute case
    if [ "${1:0:1}" = "/" ] || [ "$PWD" = "/" ]
    then
       ABS=""
    else
       ABS="$PWD"
    fi

    # loop thru path
    IFS="/"
    for DIR in $1
    do
       if [ -n "$DIR" ]
       then
          if [ "$DIR" = ".." ]
          then
             ABS="${ABS%/*}"

          elif [ "$DIR" != "." ]
          then
             ABS="$ABS/$DIR"
          fi
       fi
    done
    IFS=":"

    echo $ABS
    return 0
} 

######################################################################
echo
echo "UPP NYGHTY2DEB BUILDER"
echo "CopyRight(c) 2009 By Massimo Del Fedele"
echo

# checks for parameters
if [ x"$1" = x -o x"$2" = x ]
then
	echo "uppnighty2deb <upp root> <dest folder>"
	exit 1
fi

#sets up source and dest folder names
src=$(rel2abs $1)
dst=$(rel2abs $2)

#locates 'uppdeb' and 'idebuild' scripts
uppdeb="`which uppdeb`"
if [ x$uppdeb = x ]
then
	# couldn't find it on path, just try on this script's path
	LSOF=$(lsof -p $$ 2>/dev/null | grep -E "/"$(basename $0)"$")
	uppdeb=$(echo $LSOF | sed -r s/'^([^\/]+)\/'/'\/'/1 2>/dev/null)
	uppdeb=$(dirname $uppdeb)/uppdeb
fi
if [ ! -f $uppdeb ]
then
	echo "ERROR - Could not find 'uppdeb' script"
	echo "Please check if it's on current path"
	exit 1
fi
idebuild="`which idebuild`"
if [ x$idebuild = x ]
then
	# couldn't find it on path, just try on this script's path
	LSOF=$(lsof -p $$ 2>/dev/null | grep -E "/"$(basename $0)"$")
	idebuild=$(echo $LSOF | sed -r s/'^([^\/]+)\/'/'\/'/1 2>/dev/null)
	idebuild=$(dirname $idebuild)/idebuild
fi
if [ ! -f $idebuild ]
then
	echo "ERROR - Could not find 'idebuild' script"
	echo "Please check if it's on current path"
	exit 1
fi

#checks if source folder is an upp folder
for name in uppsrc examples tutorial reference bazaar
do
	if [ ! -d $src/$name ]
	then
		echo "bad source folder '$src'"
		echo "missing '$src/$name' subfolder"
		exit 1
	fi
done

#gets release number from uppsrc/ide/version.h
release="`cat $src/uppsrc/ide/version.h`"
release=${release#*\"}
release=${release%%\"*}
echo "building .deb for nighty build $release"

#creates a temporary folder for executable file
outtmp="`mktemp -t -d uppnighty2deb.XXXXXXX`"

#build theide
echo "building TheIDE......"
bash $idebuild $src $outtmp/theide
if [ $? = 0 ]
then
	true
else
	echo "Error building TheIDE"
	echo "Please check executables for errors"
	exit 1
fi

#creates the deb package
echo "Creating package......"
bash $uppdeb $src $outtmp/theide $dst $release
result=$?

#remove temporary folder
rm -rf $outtmp
